package com.mxgraph.examples.swing;
//import ScrollBarExample;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//import ScrollBarExample.MyAdjustmentListener;

public class SearchFile extends JPanel
{
	// JLabel searchDirectoryPathLabel;
  private String fileNameToSearch;
  private List<String> result = new ArrayList<String>();
 /* public SearchFile () {
      super(true);
      searchDirectoryPathLabel=new JLabel();
      setLayout(new BorderLayout());

      JScrollBar hbar=new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
      JScrollBar vbar=new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300);

      hbar.setUnitIncrement(2);
      hbar.setBlockIncrement(1);

      //hbar.addAdjustmentListener(new MyAdjustmentListener());
      //vbar.addAdjustmentListener(new MyAdjustmentListener());

      add(hbar, BorderLayout.SOUTH);
      add(vbar, BorderLayout.EAST);
      add( searchDirectoryPathLabel);
  }*/

  public String getFileNameToSearch() 
  {
	return fileNameToSearch;
  }
  public void setFileNameToSearch(String fileNameToSearch) 
  {
	this.fileNameToSearch = fileNameToSearch;
  }
 
  public List<String> getResult()
  {
	return result;
  }
 
  public List searchSwitchFile(File SelectedFile )
  {
	  JFrame frame = new JFrame("Scroll Bar Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setContentPane(new SearchFile());
      frame.setSize(200,200);
      frame.setVisible(true);
		ArrayList<String> switchIPs = new ArrayList<String>();
try{

	String matchedResult = null;
	
	SearchFile fileSearch = new SearchFile();
         //try different directory and filename :)
	fileSearch.searchDirectory(new File(SelectedFile.getAbsolutePath()), "switches.conf");
 
	int count = fileSearch.getResult().size();
	if(count ==0)
	{
	    System.out.println("\nNo result found!");
	}
	else
	{
	    System.out.println("\nFound " + count + " result!\n");
	    
	    for (String matched : fileSearch.getResult())
	    {
	    	matchedResult = matched;
		System.out.println("Found : " + matched);
	    }
	    FileInputStream fstream = new FileInputStream(matchedResult);
	    
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String strLine;
	    
	    while ((strLine = br.readLine()) != null) 
	    {
	  	  if (strLine.startsWith("["))
	  	{
	  		switchIPs.add(strLine);
	   // System.out.println (strLine);
	    }
	    }
	}
  }catch (Exception e)
  {
	  e.printStackTrace();
	  } 
return switchIPs;
  }
 
  public void searchDirectory(File directory, String fileNameToSearch)
  {
 
	setFileNameToSearch(fileNameToSearch);
 
	if (directory.isDirectory()) 
	{
	    search(directory);
	} 
	else
	{
	    System.out.println(directory.getAbsoluteFile() + " is not a directory!");
	}
 
  }
 
  private void search(File file) 
  {
 try
 {
	  
	if (file.isDirectory()) 
	{
		//searchDirectoryPathLabel.setText(file.getAbsolutePath());
        //  repaint();
	 System.out.println("Searching directory ... " + file.getAbsoluteFile());
 
            //do you have permission to read this directory?	
	    if (file.canRead()) 
	    {
		for (File temp : file.listFiles()) 
		{
		    if (temp.isDirectory())
		    {
			search(temp);
		    } else {
			if (getFileNameToSearch().equals(temp.getName().toLowerCase()))
			{			
			    result.add(temp.getAbsoluteFile().toString());
		    }
 		}
	    }
 	 }
	    else 
	 {
		System.out.println(file.getAbsoluteFile() + "Permission Denied");
	 }
      }
	
 }
 catch (Exception e)
 {
e.printStackTrace();
} 
  }
 }
